using System;
using WRSManagementSystem.DomainModel.Entities;
using WRSManagementSystem.DomainModel.Repositories;

namespace WRSManagementSystem.DomainModel.Services
{
    public class TaskAssignmentService : ITaskAssignmentService
    {
        private readonly IPeopleRepository _pepRepository;
        private readonly ITasksRepository _tasksRespsoitory;

        public TaskAssignmentService(IPeopleRepository peoplerepo, ITasksRepository tasksrepo)
        {
            _pepRepository = peoplerepo;
            _tasksRespsoitory = tasksrepo;
        }

        public void AssignExistingTask(int creatorId, int personId, int taskId)
        {
            Task task = _tasksRespsoitory.FindOne(t => t.Id == taskId);
            Person person = _pepRepository.FindOne(p => p.Id == personId);

            if (task.Project.Creator.Id != creatorId)
                throw new Exception("You are not allowed to assign people to this task");

            task.PeopeInvolved.Add(person);

            //TODO: changes should be saved in session-like fashion
            _tasksRespsoitory.Save();
        }

        public void AssignExistingTaskToMe(int personId, int taskId)
        {
            Task task = _tasksRespsoitory.FindOne(t => t.Id == taskId);
            Person person = _pepRepository.FindOne(p => p.Id == personId);

            task.PeopeInvolved.Add(person);

            //TODO: changes should be saved in session-like fashion
            _tasksRespsoitory.Save();
        }
    }
}